package org.viettan.android.main.activities.campaign;

import java.util.List;

import org.viettan.android.main.R;
import org.viettan.android.main.activities.common.RefreshableListActivity;
import org.viettan.android.main.intents.IntentKeys;
import org.viettan.android.main.io.DownloadEntityTask;
import org.viettan.android.main.io.EntityRetrievalException;
import org.viettan.android.main.managers.CampaignManager;
import org.viettan.android.main.managers.DialogManager;
import org.viettan.android.main.managers.SettingsManager;
import org.viettan.android.main.model.Campaign;
import org.viettan.android.main.model.Language;
import org.viettan.android.main.view.CampaignAdapter;
import org.viettan.android.main.view.CampaignAdapterFactory;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.google.inject.Inject;

/**
 * Displays the ongoing Viet Tan campaigns.
 */
public class CampaignListActivity extends RefreshableListActivity {
    @Inject
    private CampaignManager campaignManager;
    @Inject
    private DialogManager notificationManager;
    @Inject
    private CampaignAdapterFactory campaignAdaptorFactory;
    @Inject
    private SettingsManager settingsManager;

    private Language lastLanguage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	loadList(true);
    }

    @Override
    protected void loadList(boolean useCache) {
	lastLanguage = settingsManager.getArticleLanguage();
	new DownloadCampaignsTask(this, notificationManager, useCache).execute();
    }

    @Override
    protected void onRestart() {
	super.onRestart();
	if (settingsManager.hasLanguageChanged(lastLanguage)) {
	    loadList(true);
	}
    }

    private void displayCampaigns(List<Campaign> campaigns) {
	if (campaigns == null) {
	    return;
	}
	CampaignAdapter adapter = campaignAdaptorFactory.create(this, R.layout.campaign_row,
		campaigns);
	setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView listView, View view, int position, long id) {
	Campaign campaign = (Campaign) getListAdapter().getItem(position);
	Intent sectionIntent = new Intent(this, CampaignActivity.class);
	sectionIntent.putExtra(IntentKeys.CAMPAIGN_ID.name(), campaign.getId());
	startActivity(sectionIntent);
    }

    private static class DownloadCampaignsTask extends
	    DownloadEntityTask<CampaignListActivity, List<Campaign>> {

	private final boolean useCache;

	private DownloadCampaignsTask(CampaignListActivity campaignListActivity,
		DialogManager notificationManager, boolean useCache) {
	    super(campaignListActivity, notificationManager);
	    this.useCache = useCache;
	}

	@Override
	protected List<Campaign> getEntity() throws EntityRetrievalException {
	    CampaignListActivity activity = getActivity();
	    if (activity == null) {
		return null;
	    }
	    return activity.campaignManager.getCampaigns(useCache);
	}

	@Override
	protected void displayEntity(List<Campaign> campaigns) {
	    CampaignListActivity activity = getActivity();
	    if (activity == null) {
		return;
	    }
	    activity.displayCampaigns(campaigns);
	}
    }
}
